<template>
    <a-modal
        v-model:visible="showModal"
        :title="title"
        :width="380"
        :destroyOnClose="true"
        :maskClosable="false"
        :confirmLoading="loading"
        @ok="onSubmit"
        @cancel="handleCancle"
    >
        <a-form class="dept-model" layout="horizontal" :model="formState" :rules="rules" ref="formRef">
            <a-form-item class="full-width" name="picTypeClass">
                <a-select v-model:value="formState.picTypeClass" :placeholder="t('yl.materialClassLabel')">
                    <a-select-option :value="1">视频素材</a-select-option>
                    <a-select-option :value="0">图片素材</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item class="full-width" name="picTypeCname">
                <a-input v-model:value="formState.picTypeCname" :placeholder="t('yl.materialNameLabel')" />
            </a-form-item>
        </a-form>
    </a-modal>
</template>
<script lang="ts">
import { computed, defineComponent, reactive, ref, watch } from 'vue';
import { addMaterialType } from '@/api/material';

import { useI18n } from 'vue-i18n';
import _ from 'lodash';

// 表单校验规则
const initRules = (t: fn) => {
    return {
        picTypeClass: [{ required: true, message: t('yl.materialClassLabel'), trigger: 'blur' }],
        picTypeCname: [{ required: true, message: t('yl.materialNameLabel'), trigger: 'blur' }],
    };
};

export default defineComponent({
    name: 'roleModel',
    components: {},
    props: {
        visible: {
            type: Boolean,
            default: () => false,
        },
        title: {
            type: String,
        },
        selectEditModel: {
            type: Array,
            default: () => [],
        },
        loading: {
            type: Boolean,
            default: () => false,
        },
        picTypeClass: {
            type: String,
            default: () => '',
        },
        picTypeCname: {
            type: String,
            default: () => '',
        },
        classType: {
            type: String,
            default: () => 'add',
        },
    },
    emits: ['submit', 'cancle'],
    setup(props, { emit }) {
        const { t } = useI18n();
        const showModal = computed(() => props.visible);
        let models = computed(() => props.selectEditModel as NormalObj[]);
        const formState = reactive({
            picTypeClass: props.picTypeClass ? props.picTypeClass : null,
            picTypeCname: props.picTypeCname,
            picTypeEname: '',
        });
        const classType = props.classType;

        // 表单校验
        const rules = initRules(t);
        const formRef = ref();
        // 提交
        const onSubmit = () => {
            console.log(classType);
            formRef.value.validate().then(() => {
                addMaterialType(formState).then((res) => {
                    formRef.value.resetFields();
                    emit('submit', 'submit');
                });
            });
        };

        // 取消
        const handleCancle = () => {
            return emit('submit');
        };

        return {
            formRef,
            labelCol: { span: 8 },
            wrapperCol: { span: 16 },
            showModal,
            models,
            formState,
            t,
            rules,
            onSubmit,
            handleCancle,
            classType,
        };
    },
});
</script>
<style lang="scss" scoped>
.dept-model {
    @include flexWrap;
    width: 100%;
    .ant-form-item {
        @include flex;
        width: 50%;
        margin-right: 0;
        padding-right: $width10;
        margin-bottom: $width8;
        :deep .ant-form-item-label {
            flex-basis: $formLabel;
        }
        :deep .ant-form-item-control-wrapper {
            flex: 1;
        }
        :deep .ant-form-item-children,
        :deep .ant-input-number {
            width: 100%;
        }
        &.full-width {
            width: 100%;
        }
    }
}
</style>
